import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
class Carousel extends StatefulWidget {
  @override
  _CarouselState createState() => _CarouselState();
}
class carouselItem{
  String imgUrl;
  String pageUrl;
  carouselItem({this.imgUrl,this.pageUrl});
}
List carouselItems=[
  carouselItem(imgUrl:'',pageUrl:''),
  carouselItem(imgUrl:'',pageUrl:''),
  carouselItem(imgUrl:'',pageUrl:''),
];
Widget carouselItemContainer(item){
  return GestureDetector(
    onTap: (){},
    child: CachedNetworkImage(
      imageUrl:
      'https://mlxybk-exchange-public.oss-cn-hongkong.aliyuncs.com/hryfile/e/1/27a07bbec7a7412f969467b5b5de5506.png',
      fit: BoxFit.fill,
    ),
  );
}
class _CarouselState extends State<Carousel> {
  int _currentPageIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 15.0),
      height: 160,
      child: Stack(
        children: [
          ClipRRect(
            borderRadius: BorderRadius.circular(7),
            child: PageView(
              onPageChanged: (int index) {
                setState(() {
                  _currentPageIndex = index % (carouselItems.length);
                });
              },
              children: carouselItems.map((item) {
                return carouselItemContainer(item);
              }).toList(),
            ),
          ),
          Positioned(
            bottom: 10,
            left: 0,
            right: 0,
            child: Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(carouselItems.length, (i) {
                    return Container(
                      margin: EdgeInsets.symmetric(horizontal: 5),
                      width: _currentPageIndex == i ? 20.0 : 10.0,
                      height: 2.5,
                      decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(5.0),
                          color:
                          _currentPageIndex == i ? Colors.white : Colors.grey),
                    );
                  }).toList(),
                )),
          )
        ],
      ),
    );
  }
}
